package com.fs.web.base.command;

import com.fs.common.baseMapper.ModleMapper;
import com.fs.common.pojo.Disease;
import com.fs.common.pojo.Modle;
import com.fs.common.pojo.WechatPayConfig;
import com.fs.common.utils.ConcurrentHashMapCacheUtils;
import com.fs.common.yzMapper.DiseaseMapper;
import com.fs.common.yzMapper.WechatPayConfigMapper;
import com.fs.web.base.param.SysConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author TiddlerCJ
 * @desc 加载模块数据
 */
@Slf4j
@Component
@Order(value=1)
public class LoadModleDataCommand implements CommandLineRunner {
	@Autowired
    private ModleMapper modleMapper;
	@Autowired
	private DiseaseMapper diseaseMapper;

	@Autowired
	private WechatPayConfigMapper wechatPayConfigMapper;
	@Override
	public void run(String... args) throws Exception {
		/*
		 * 加载Modle所有数据
		 */
		List<Modle> modleList = modleMapper.selectList(null);
		for(Modle m: modleList){
			SysConstant.MODLE_CLASSNAME.put(m.getModleName(), m.getClassName());
		}
		loadDiseaseToCache();
		cacheWechatPayConfig();
		log.info(" Load ALL Data success! ");
	}

	private void loadDiseaseToCache(){
		List<Disease> diseaseList = diseaseMapper.selectList(null);
		//将症状相关信息加入缓存中
		for (Disease d : diseaseList) {
			SysConstant.DISEASE_CODE_NAME.put(d.getCode(),d.getName());
		}
	}

	private void cacheWechatPayConfig(){
		WechatPayConfig wechatPayConfig = wechatPayConfigMapper.selectById(1);
		if (wechatPayConfig != null) {
			ConcurrentHashMapCacheUtils.setCache(WechatPayConfig.CACHE_KEY,wechatPayConfig);
		}
	}
}
